package com.mushuilingfeng.ddd.user.domain.user.entity;

import com.mushuilingfeng.ddd.framework.factory.DomainRepositoryFactory;
import com.mushuilingfeng.ddd.user.domain.base.BaseEntity;
import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.base.Ii8nCode;
import com.mushuilingfeng.ddd.user.domain.user.repository.IRegisterInviteCodeRepository;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

/**
 * @author 沐水淋风
 * @date 2024/10/6
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class RegisterInviteCode extends BaseEntity {
    private String inviteCode;
    private Long ownerUserId;
    private Long registerUserId;
    private Date usedTime;
    
    public void checkCanUse() {
        if (registerUserId != null) {
            throw new BusinessException(Ii8nCode.INVITE_CODE_ERROR);
        }
    }
    
    public void use(Long registerUserId) {
        this.registerUserId = registerUserId;
        this.usedTime = new Date();
        DomainRepositoryFactory.getRepository(IRegisterInviteCodeRepository.class).updateRegisterUser(id, registerUserId);
    }
    
    @Override
    public void saveNew() {
        throw new UnsupportedOperationException("not support saveNew method!");
    }
    
    @Override
    public void update() {
        throw new UnsupportedOperationException("not support update method!");
    }
    
}
